import Cookies from 'js-cookie'

const TOKEN_KEY = '_TPL__Token' // cookie的key
const DEFAULT_DUR = 1 // cookie存储时长， 单位 天

const USER_PREFIX = '_TPL__user'
const MENU_PREFIX = '_TPL__menu'
const PERMISSTION_PREFIX = '_TPL_permission'

export const User = {
  get: function (key) {
    if (key == null) {
      let user = null
      for (const k in sessionStorage) {
        if (k.indexOf(USER_PREFIX + '_') === 0) {
          if (user === null) {
            user = {}
          }
          user[k.split(USER_PREFIX + '_')[1]] = sessionStorage.getItem(k)
        }
      }
      return user
    } else {
      return sessionStorage.getItem(USER_PREFIX + '_' + key)
    }
  },
  set: function (user) {
    if (typeof user !== 'object') {
      console.error('localuser: user must is Object')
      return
    }
    // 设置之前，先清空
    this.remove()
    for (const key in user) {
      // 过滤为null参数
      if (user[key] != null) {
        sessionStorage.setItem(USER_PREFIX + '_' + key, user[key])
      }
    }
  },
  setItem: function (key, value) {
    if (typeof key !== 'string') {
      console.error('local: user`s key must is String')
      return
    }
    if (typeof value !== 'string' && typeof value !== 'number') {
      console.error('local: user`s value must is String or Number')
      return
    }
    sessionStorage.setItem(USER_PREFIX + '_' + key, value)
  },
  remove: function() {
    const user = this.get()
    for (const key in user) {
      sessionStorage.removeItem(USER_PREFIX + '_' + key)
    }
  },
  removeItem: function (key) {
    if (typeof key !== 'string') {
      console.error('local: user`s key must is String')
      return
    }
    if (typeof value !== 'string' && typeof value !== 'number') {
      console.error('local: user`s value must is String or Number')
      return
    }
    sessionStorage.removeItem(USER_PREFIX + '_' + key)
  }
}

export const Token = {
  get: function() {
    return Cookies.get(TOKEN_KEY)
  },
  set: function(token, duration) {
    return Cookies.set(TOKEN_KEY, token, { expires: duration || DEFAULT_DUR })
  },
  remove: function() {
    return Cookies.remove(TOKEN_KEY)
  }
}

export const Menu = {
  get: function() {
    const menu = sessionStorage.getItem(MENU_PREFIX)
    if (menu != null) {
      return JSON.parse(menu)
    }
    return null
  },
  set: function(menu) {
    if (typeof menu === 'object') {
      menu = JSON.stringify(menu)
    }
    sessionStorage.setItem(MENU_PREFIX, menu)
  },
  remove: function() {
    sessionStorage.removeItem(MENU_PREFIX)
  }
}

export const Permission = {
  get: function() {
    const permission = sessionStorage.getItem(PERMISSTION_PREFIX)
    if (permission != null) {
      return JSON.parse(permission)
    }
    return null
  },
  set: function(permission) {
    if (typeof permission === 'object') {
      permission = JSON.stringify(permission)
    }
    sessionStorage.setItem(PERMISSTION_PREFIX, permission)
  },
  remove: function() {
    sessionStorage.removeItem(PERMISSTION_PREFIX)
  }
}